<?php

namespace App\Http\Resources;

use App\Models\CommentModel;
use Illuminate\Http\Resources\Json\JsonResource;

class CommentModelResource extends JsonResource
{
    public function toArray($request): array
    {
        $comment = [
            'id'            => $this->id,
            'did'           => $this->did,
            'pid'           => $this->pid,
            'uid'           => $this->uid,
            'comment'       => $this->comment,
            'username'      => $this->username,
            'nickname'      => $this->nickname,
//            'created_at'    => $this->created_at,
        ];
        if ($this->pid) {
            $parentComment = CommentModel::where('id', $this->pid)->select(['username', 'nickname'])->first();
            if ($parentComment) {
                $comment['to_user'] = $parentComment->username;
                $comment['to_nickname'] = $parentComment->nickname;
            }
        }
        return $comment;
    }
}
